<?php
	require_once('../includes/config.php');
	$action = "add";
	$statusbtn = "Add new";
	$readonly = "";
	$proname = "";
	$procat  = "";
	$proid = "";
	$prodetail = "";
	$prospecial = "";
	if($_REQUEST['act'] == "edit")
	{
		$proid = $_GET['id'];
		$statusbtn = "Update";
		$readonly = "readonly";
		$str_query = "SELECT * FROM tbl_product WHERE id = '$proid'";
		$resul = mysql_query("$str_query") or die ("can not load data for update ");
		$data = mysql_fetch_assoc($resul);
		$procat = $data['pro_cat'];
		$proname = $data['pro_name'];
		$proimg = $data['pro_img'];
		$prodetail = $data['pro_detail'];
		$action = "edit";
		
	}
?>
<script type="text/javascript">
$(document).ready(function() {
	var name 		= $('#pro_name');
	var nameInfo	= $('#nameInfo');
	var urlimg 		= $('#pro_image');
	var imgInfo 	= $('#imgInfo');
	var detail 		= $('#pro_detail');
	var detailInfo = $('detailInfo');
	
	name.blur(valName);
	urlimg.blur(valUrl);
	detail.blur(valDetail);
	
	name.keyup(valName);
	urlimg.keyup(valUrl);
	detail.keyup(valDetail);
	
	function valName(){
		if(name.val().length < 3 || name.val().length > 20) {
			nameInfo.text("bettwen 3 - 20 char");
			nameInfo.removeClass("isok");
			nameInfo.addClass("error");
			return false;
		}
		else {
			nameInfo.text("Its look good");
			nameInfo.removeClass('error');
			nameInfo.addClass('isok');
			return true;
		}
	}
	
	function valUrl(){
		var imageRegex = /(.*?)\.(jpg|jpeg|png|gif)$/;
		var x = urlimg.val();
		if(x != "" && imageRegex.test(x)){
			imgInfo.text('Its OK');
			imgInfo.removeClass('error');
			imgInfo.addClass('isok');
			return true;
		}
		else {
			imgInfo.text('Please check your link');
			imgInfo.removeClass("isok");
			imgInfo.addClass('error');
			return false;	
		}
	}
	function valDetail(){
		if(encodeURIComponent(detail.val()).length < 10){
			detailInfo.addClass('error');
			detailInfo.text('Must more than 10 char');
			return false;
		}
		else {
			detailInfo.removeClass('error');
			detailInfo.addClass('isok');
			detailInfo.text('OK!');
			return true;	
		}
		
	}
	$("form#proForm").submit(function(){
		if(valName() && valUrl() && valDetail()){
			var id = $('#pro_id').attr('value');
			var cat = $('#pro_cat').attr('value');
			var act =  $('#act').attr('value');
			var datastring = 'act=' + act +
							 '&id=' + id +
							 '&cat=' + cat +
							 '&name=' + name.attr('value') +
							 '&urlimg=' + urlimg.attr('value') +
							 '&detail=' + encodeURIComponent(detail.attr('value'));
			 $.ajax({ 
				 url: "product_process.php",
				 type: "POST",
				 data: datastring,
				 dataType: 'json',
				 success: function(data){
					 if(data==1)
					 {
						$("#rounded-corner").load("product_list.php");
						disablePopup();
						$("form").each( function() { this.reset; });
					 }
					 else
					 {
						 alert('Please check your input!');
					 }
				 }
			 });
			 return false;
		}
		else
			return false;
	});
});
</script>
<link rel="stylesheet" href="templates/redactor/css/redactor.css" />
<script src="templates/redactor/redactor.js"></script>
<script type="text/javascript"> 
	$(document).ready(
		function()
		{
			$('#pro_detail').redactor({ focus: true });
			$('#pro_detail').redactor({ autoresize: false });
		}
	);
</script>	
		<a id="popupProductClose">x</a>
		<h1><?php echo $action; ?> Product</h1>
        <form id="proForm" method="post">
        <input type="hidden" id="pro_id" value="<?php echo $id; ?>" />
        <p>
          <label>Product Name:</label>
          <input type="text" name="pro_name" id="pro_name" value="<?php echo $proname; ?>" />
          <span id="nameInfo"></span>
        </p>
        <p>
          <label>Product Category:</label>
              <select name="pro_cat" id="pro_cat">
              <?php 
			  		echo _GetCatList($procat);
			  ?>
              </select>
        </p>
        <p>
          <label>Product Image:</label>
          <input type="text" name="pro_image" id="pro_image" value="<?php echo $proimg; ?>" />
          <span id="imgInfo"></span>
        </p>
        <fieldset>
        <legend>Detail</legend>
        <p>
          <textarea id="pro_detail" name="pro_detail" cols="20" rows="5" style="height: 236px;"><?php echo $prodetail; ?>
          </textarea>
          <p id="detailInfo"></p>
        </p>
        </fieldset>
        <button id="act" type="submit" name="act" value="<?php echo $action; ?>">Save</button>
        </form>
		
